using Microsoft.AspNetCore.Mvc;
using ZR.Admin.WebApi.Filters;
using ZR.Model;
using ZR.Model.System;
using ZR.Model.System.Dto;
using ZR.ServiceCore.Services.IService;

namespace ZR.Admin.WebApi.Controllers.System
{
    /// <summary>
    /// 风力数据DBS查询管理
    /// </summary>
    [Verify]
    [Route("system/windDataDBS")]
    [ApiExplorerSettings(GroupName = "sys")]
    public class WindDataDBSController : BaseController
    {
        /// <summary>
        /// 风力数据DBS查询服务
        /// </summary>
        private readonly IWindDataDBSService WindDataDBSService;

        public WindDataDBSController(IWindDataDBSService windDataDBSService)
        {
            WindDataDBSService = windDataDBSService;
        }

        /// <summary>
        /// 查询风力数据DBS查询列表
        /// </summary>
        /// <param name="request">查询参数</param>
        /// <returns></returns>
        [HttpPost("list")]
        public IActionResult List([FromBody] WindDataDBSListRequest request)
        {
            var query = new WindDataDBSQueryDto
            {
                Id = request.Id,
                SourceTable = request.SourceTable,
                Height = request.Height,
                WindDirection = request.WindDirection,
                WindSpeed = request.WindSpeed,
                VerticalSpeed = request.VerticalSpeed,
                BeginTime = request.BeginTime,
                EndTime = request.EndTime,
                TimeParam = request.TimeParam
            };
            var pager = new PagerInfo
            {
                PageNum = request.PageNum,
                PageSize = request.PageSize
            };
            var list = WindDataDBSService.SelectWindDataDBSList(query, pager);
            return SUCCESS(list);
        }

        /// <summary>
        /// 查询风力数据DBS查询详情
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        [HttpGet("{id}")]
        [ActionPermissionFilter(Permission = "system:windDataDBS:query")]
        public IActionResult GetInfo(int id)
        {
            var query = new WindDataDBSQueryDto { Id = id };
            var pager = new PagerInfo { PageNum = 1, PageSize = 1 };
            var result = WindDataDBSService.SelectWindDataDBSList(query, pager);
            var response = result.Result?.FirstOrDefault();
            return SUCCESS(response);
        }

        /// <summary>
        /// 模糊查询风力数据DBS查询列表
        /// </summary>
        /// <param name="request">查询参数</param>
        /// <returns></returns>
        [HttpPost("listWithLike")]
        [ActionPermissionFilter(Permission = "system:windDataDBS:list")]
        public IActionResult ListWithLike([FromBody] WindDataDBSListRequest request)
        {
            var query = new WindDataDBSQueryDto
            {
                Id = request.Id,
                SourceTable = request.SourceTable,
                Height = request.Height,
                WindDirection = request.WindDirection,
                WindSpeed = request.WindSpeed,
                VerticalSpeed = request.VerticalSpeed,
                BeginTime = request.BeginTime,
                EndTime = request.EndTime,
                TimeParam = request.TimeParam
            };
            var pager = new PagerInfo
            {
                PageNum = request.PageNum,
                PageSize = request.PageSize
            };
            var list = WindDataDBSService.SelectWindDataDBSListWithLike(query, pager);
            return SUCCESS(list);
        }

        /// <summary>
        /// 获取风力数据DBS查询统计信息
        /// </summary>
        /// <returns></returns>
        [HttpGet("statistics")]
        [ActionPermissionFilter(Permission = "system:windDataDBS:list")]
        public IActionResult GetStatistics()
        {
            var statistics = WindDataDBSService.GetWindDataDBSStatistics();
            return SUCCESS(statistics);
        }

        /// <summary>
        /// 根据时间参数获取DBS表名列表
        /// </summary>
        /// <param name="timeParam">时间参数</param>
        /// <returns></returns>
        [HttpGet("tables/{timeParam}")]
        [ActionPermissionFilter(Permission = "system:windDataDBS:list")]
        public IActionResult GetDBSTables(string timeParam)
        {
            var tables = WindDataDBSService.GetDBSTableNames(timeParam);
            return SUCCESS(tables);
        }

        /// <summary>
        /// 获取所有DBS表名列表
        /// </summary>
        /// <returns></returns>
        [HttpGet("tables")]
        [ActionPermissionFilter(Permission = "system:windDataDBS:list")]
        public IActionResult GetAllDBSTables()
        {
            var tables = WindDataDBSService.GetAllDBSTableNames();
            return SUCCESS(new { 
                TableCount = tables.Count, 
                Tables = tables,
                Message = $"找到 {tables.Count} 个DBS表"
            });
        }

        /// <summary>
        /// 导出风力数据DBS查询
        /// </summary>
        /// <param name="request">查询参数</param>
        /// <returns></returns>
        [HttpPost("export")]
        [ActionPermissionFilter(Permission = "system:windDataDBS:export")]
        [Log(Title = "风力数据DBS查询", BusinessType = BusinessType.EXPORT)]
        public IActionResult Export([FromBody] WindDataDBSListRequest request)
        {
            var query = new WindDataDBSQueryDto
            {
                Id = request.Id,
                SourceTable = request.SourceTable,
                Height = request.Height,
                WindDirection = request.WindDirection,
                WindSpeed = request.WindSpeed,
                VerticalSpeed = request.VerticalSpeed,
                BeginTime = request.BeginTime,
                EndTime = request.EndTime,
                TimeParam = request.TimeParam
            };
            var pager = new PagerInfo
            {
                PageNum = 1,
                PageSize = 10000 // 导出时限制最大数量
            };
            var list = WindDataDBSService.SelectWindDataDBSList(query, pager);
            var result = ExportExcelMini(list.Result, "windDataDBS", "风力数据DBS查询");
            return ExportExcel(result.Item2, result.Item1);
        }
    }
}